﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RapidTestMapMaker
{
    public partial class PalleteUserControl : UserControl
    {
        public PalleteUserControl()
        {
            InitializeComponent();
            swatches = new PalleteSwatchUserControl[]
            {
                palleteSwatchUserControl1, 
                palleteSwatchUserControl2,
                palleteSwatchUserControl3,
                palleteSwatchUserControl4,
                palleteSwatchUserControl5,
                palleteSwatchUserControl6,
                palleteSwatchUserControl7,
                palleteSwatchUserControl8,
                palleteSwatchUserControl9,
                palleteSwatchUserControl10
            };
        }

        private PalleteSwatchUserControl[] swatches;
        private TileSet _tileset;
        public TileSet TileSet
        {
            get { return _tileset; }
            set
            {
                _tileset = value;
                if (value != null)
                {
                    while (_tileset.Tiles.Count < 10)
                    { _tileset.AddTile(new Tile()); }
                    UpdateTileImages();
                    _tileset.TileChanged += _tileset_TileChanged;
                }
            }
        }

        void _tileset_TileChanged(object sender, EventArgs e)
        {
            UpdateTileImages();
        }

        private void UpdateTileImages()
        {
            int i = 0;
            if (_tileset != null && _tileset.Tiles != null)
            {
                for (; i < Math.Min(_tileset.Tiles.Count(), swatches.Length); i++)
                {
                    swatches[i].TileSet = _tileset;
                    swatches[i].Tile = _tileset.Tiles[i];
                }
            }
            for (; i < swatches.Length; i++)
            {
                swatches[i].Tile = null;
            }
        }
    }
}
